package com.eastfair.projectcore.service.impl;

import com.eastfair.projectcore.dao.HotImagePointMapper;
import com.eastfair.projectcore.entity.HotImagePoint;
import com.eastfair.projectcore.service.HotImagePointService;
import com.eastfair.projectcore.vo.HotImagePointVO;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.projectcore.HotImageCacheKeyBuilder;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;

import com.eastfair.uid.model.SelfUidDto;
import com.eastfair.uid.service.SelfUidService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 电子橱窗点位表
 * </p>
 *
 * @author ligang
 * @date 2021-08-09
 */
@Slf4j
@Service
public class HotImagePointServiceImpl extends SuperCacheServiceImpl<HotImagePointMapper, HotImagePoint> implements HotImagePointService {

    @Autowired
    SelfUidService selfUidService;
    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(HotImagePoint model) {
        //雪花ID,不需要分库分表
        SelfUidDto selfUidDto = new SelfUidDto();
        model.setId(selfUidService.getUid(selfUidDto));
        return R.successDef();
    }

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new HotImageCacheKeyBuilder();
    }


    @Override
    public List<HotImagePointVO> findIdAll(Long hotImgId) {
        HotImagePoint hotImagePoint = new HotImagePoint();
        hotImagePoint.setHotImageId(hotImgId);
        hotImagePoint.setDeleted(BusinessConstant.DELETE_NO);
        QueryWrap<HotImagePoint> wrapper = Wraps.q(hotImagePoint);
        List<HotImagePoint>  hotImagePointList = list(wrapper);
        return doToVos(hotImagePointList);
    }




    public HotImagePointVO doToVo(HotImagePoint hotImagePoint) {
        HotImagePointVO hotImagePointVo = new HotImagePointVO();
        BeanUtils.copyProperties(hotImagePoint, hotImagePointVo);
        hotImagePointVo.setId(hotImagePoint.getId());
        return hotImagePointVo;
    }

    public List<HotImagePointVO> doToVos(List<HotImagePoint> hotImagePointList) {
        if (hotImagePointList == null || hotImagePointList.isEmpty()) {
            return null;
        }
        List<HotImagePointVO> result = new ArrayList<>();
        for (HotImagePoint hotImagePoint : hotImagePointList) {
            result.add(doToVo(hotImagePoint));
        }
        return result;
    }


}
